package com.kaidi.dibankecloud.widget;

import android.app.Activity;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.widget.PopupWindow;

import com.kaidi.dibankecloud.R;
import com.kaidi.dibankecloud.databinding.PopupProtocolBinding;

/**
 * Created by Administrator on 2017/1/9.
 */

public class ProtocolPopupWindow extends PopupWindow implements View.OnClickListener {
    private Context context;
    public ProtocolPopupWindow(Context context, String content) {
        super(context);
        this.context = context;
        PopupProtocolBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.popup_protocol, null, false);
        WebSettings setting = binding.popProtocol.getSettings();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING);
        }
        setting.setUseWideViewPort(true);
        setting.setLoadWithOverviewMode(true);
        binding.popProtocol.loadData(content, "text/html; charset=utf-8;", "UTF-8");

        binding.popClose.setOnClickListener(this);
        setContentView(binding.getRoot());
        setWidth((int) context.getResources().getDimension(R.dimen.protocol_width));
        setHeight((int) context.getResources().getDimension(R.dimen.protocol_height));
        ColorDrawable dw = new ColorDrawable(0000000000);
        setBackgroundDrawable(dw);
        setOutsideTouchable(true);
        setFocusable(true);
        update();
    }
    public void showPopupWindow(View anchor){
        if(!isShowing()){
            showAtLocation(anchor, Gravity.CENTER,0,0);
            Window window = ((Activity) context).getWindow();
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.alpha = 0.5f;
            window.setAttributes(lp);
        }
    }

    @Override
    public void onClick(View view) {
        dismiss();
    }
}
